package com.wzq.upms.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


//import com.wzq.base.client.model.Unit;
//import com.wzq.base.client.unit.UnitClient;




import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.DataPermissionModel;
import com.wzq.permission.client.model.admin.OrganizeModel;
import com.wzq.permission.client.model.admin.UserModel;
import com.wzq.permission.client.service.DataPermissionService;
import com.wzq.upms.core.EasyUiData;
import com.wzq.upms.core.JsonResponse;
import com.wzq.upms.core.SessionUtils;

/**
 * 
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * 数据权限控制类
 * <p>
 * Company:
 * </p>
 * 
 * @version
 * @author qianzq
 * @date 2016年2月25日 上午9:41:55
 */
@RequestMapping("/data")
@Controller
public class DataPermissionController {

	@Autowired
	private DataPermissionService dataPerissionServiceClient;
//	@Autowired
//	private UnitClient unitClient;
	/**
	 * 数据权限页面
	 */
	private static final String ROLE_VIEW = "dataPerission/dataPerission";

	/**
	 * 用户页面
	 * 
	 * @param model
	 *            模型对象
	 * @param request
	 *            request对象
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/allList")
	public ModelAndView allList(DataPermissionModel model, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView(ROLE_VIEW);
		return mv;
	}

	/**
	 * 数据权限页面
	 * 
	 * @param model
	 *            模型对象
	 * @param request
	 *            request对象
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(DataPermissionModel model, HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView(ROLE_VIEW);
		request.setAttribute("applicationId", model.getApplicationId());
		return mv;
	}

	/**
	 * 获取数据列表
	 * 
	 * @param model
	 *            模型对象
	 * @param pager
	 *            分页对象
	 * @return EasyUiData
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	@ResponseBody
	public EasyUiData<DataPermissionModel> dataList(DataPermissionModel model, Pager pager) throws Exception {
		EasyUiData<DataPermissionModel> data = new EasyUiData<DataPermissionModel>();
		List<DataPermissionModel> dataLst = dataPerissionServiceClient.queryList(model, pager);
		data.setTotal(dataPerissionServiceClient.queryByCount(model));
		data.setRows(dataLst == null ? new ArrayList() : dataLst);
		return data;
	}

	/**
	 * 添加或修改数据
	 * 
	 * @param model
	 *            模型对象
	 * @throws Exception
	 */
	@RequestMapping("/add")
	@ResponseBody
	public JsonResponse add(DataPermissionModel model, HttpServletRequest request) throws Exception {
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS, "保存成功");
		DataPermissionModel tempDataPermissionModel = new DataPermissionModel();
		tempDataPermissionModel.setApplicationId(model.getApplicationId());
		tempDataPermissionModel.setDataPermissionName(model.getDataPermissionName());
        if(dataPerissionServiceClient.isExist(tempDataPermissionModel)>0){
            response.setMsg("该数据权限名已存在,请重新输入");
            response.setResult(JsonResponse.FAIL);
        }else{
    		UserModel role = SessionUtils.getUser(request);
    		model.setCreateUser(role.getUserName());
    		model.setUpdateUser(role.getUserName());
    		dataPerissionServiceClient.add(model);
        }
		return response;
	}

	/**
	 * 添加或修改数据
	 * 
	 * @param model
	 *            模型对象
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public JsonResponse edit(DataPermissionModel model, HttpServletRequest request) throws Exception {
		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS, "修改成功");
		DataPermissionModel tempDataPermissionModel = new DataPermissionModel();
        tempDataPermissionModel.setApplicationId(model.getApplicationId());
        tempDataPermissionModel.setDataPermissionName(model.getDataPermissionName());
        tempDataPermissionModel.setDataPermissionId(model.getDataPermissionId());
        if(dataPerissionServiceClient.isExist(tempDataPermissionModel)>0){
            response.setMsg("该数据权限名已存在,请重新输入");
            response.setResult(JsonResponse.FAIL);
        }else{
    		UserModel role = SessionUtils.getUser(request);
    		model.setUpdateTime(new Date());
    		model.setUpdateUser(role.getUserName());
    		dataPerissionServiceClient.updateBySelective(model);
        }
		return response;
	}

	/**
	 * 根据ID查找对象
	 * 
	 * @param applicationId
	 */
	@RequestMapping("/getById")
	@ResponseBody
	public DataPermissionModel getById(Long applicationId) throws Exception {
		DataPermissionModel model = dataPerissionServiceClient.queryById(applicationId);
		return model;
	}

//	/**
//	 * 根据ID查找对象
//	 * 
//	 * @param dataPermissionId
//	 */
//	@RequestMapping("/getDataByDataPermissionId")
//	@ResponseBody
//	public Map getDataByDataPermissionId(Long dataPermissionId) throws Exception {
//
//		Map dataMap = new HashMap();
//
//		DataPermissionModel model = dataPerissionServiceClient.queryById(dataPermissionId);
//		dataMap.put("model", model);
//
//		if (model.getRangeType().equals("3")) {// 市
//			Unit unit = getProvinceInfo(model.getDataValue());
//			dataMap.put("provinceCode", unit.getProvinceCode());
//			dataMap.put("cityCode", unit.getCityCode());
//
//		} else if (model.getRangeType().equals("4")) {// 区县
//			Unit unit = getCodeInfoByTownCOde(model.getDataValue());
//			dataMap.put("provinceCode", unit.getProvinceCode());
//			dataMap.put("cityCode", unit.getCityCode());
//			dataMap.put("townCode", unit.getTownCode());
//
//		} else if (model.getRangeType().equals("5")) {// 小区
//			String unitId = model.getDataValue().contains(",") ? model.getDataValue().split(",")[0]
//					: model.getDataValue();
//
//			Unit unit = getUnitInfoByUnitId(unitId);
//			dataMap.put("provinceCode", unit.getProvinceCode());
//			dataMap.put("cityCode", unit.getCityCode());
//			dataMap.put("townCode", unit.getTownCode());
//		}
//		return dataMap;
//	}
//
//	/**
//	 * 删除数据
//	 * 
//	 * @param dataPermissionIds
//	 */
//	@RequestMapping("/delete")
//	@ResponseBody
//	public JsonResponse delete(Long[] dataPermissionIds) throws Exception {
//		JsonResponse response = new JsonResponse(JsonResponse.SUCCESS, "删除成功");
//		dataPerissionServiceClient.logicDelete(dataPermissionIds);
//		return response;
//	}
//
//	/**
//	 * 获取所有省份编码和名称
//	 * 
//	 * @return
//	 */
//	@RequestMapping("/getProvince")
//	@ResponseBody
//	public List<Unit> getProvince() {
//		return unitClient.getAllprovince();
//	}
//
//	/**
//	 * 根据省份编码获取城市编码和名称
//	 * 
//	 * @param provinceCode
//	 *            省份编码
//	 * @return
//	 */
//	@RequestMapping("/getCity")
//	@ResponseBody
//	public List<Unit> getCity(String provinceCode) {
//		return unitClient.getCityByProvinecCode(provinceCode);
//	}
//
//	/**
//	 * 根据城市编码获取区县名称和编码
//	 * 
//	 * @param cityCode
//	 *            城市编码
//	 * @return
//	 */
//	@RequestMapping("/getArea")
//	@ResponseBody
//	public List<Unit> getArea(String cityCode) {
//		return unitClient.getAreaByCityCode(cityCode);
//	}
//
//	/**
//	 * 根据区县编码获取小区名称
//	 * 
//	 * @param townCode
//	 * @return
//	 */
//	@RequestMapping("/getEstate")
//	@ResponseBody
//	public List<Unit> getEstate(String townCode) {
//		return unitClient.getUnitListByTown(townCode);
//	}
//
//	/**
//	 * 根据市编码获取省信息
//	 * 
//	 * @param cityCode
//	 *            市编码
//	 * @return
//	 */
//	@RequestMapping("/getProvinceInfo")
//	@ResponseBody
//	public Unit getProvinceInfo(String cityCode) {
//		return unitClient.getprovinceInfo(cityCode);
//	}
//
//	/**
//	 * 根据区县编码获取省市信息
//	 * 
//	 * @param townCode
//	 *            区县编码
//	 * @return
//	 */
//	@RequestMapping("/getCodeInfo")
//	@ResponseBody
//	public Unit getCodeInfoByTownCOde(String townCode) {
//		return unitClient.getCodeInfo(townCode);
//	}
//
//	/**
//	 * 根据小区编码获取省市区信息
//	 * 
//	 * @param UnitId
//	 *            小区id
//	 * @return
//	 */
//	@RequestMapping("/getUnitInfo")
//	@ResponseBody
//	public Unit getUnitInfoByUnitId(String UnitId) {
//		return unitClient.getUnit(UnitId);
//	}

}
